// chat.js
// 访问模型服务
const axios = require("axios");
// 这里就是你的accessToken，我改了两个数，所以你得替换成自己的喽~
const accessToken =
  "24.9939d47f27d8d15261bc173f46b65702.2592000.1713874883.282335-58008667";
const ERNIEB4 =
  "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro";
const ERNIEB =
  "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions";

class Conversation {
  constructor() {
    // 上下文数据存在这里，文心的调用是需要把所有的历史对话数据全部传过去，所以上下文窗口大小得注意
    this.messages = [];
  }

  async ask(prompt) {
    // 问句push进去
    this.messages.push({ role: "user", content: prompt });
    console.log("message" + this.messages[0]);
    try {
      const res = await axios.post(
        ERNIEB,
        { messages: this.messages },
        { params: { access_token: accessToken } }
      );
      const { data } = res;
      console.log(data);
      // 答案也放进去
      this.messages.push({ role: "assistant", content: data.result });
      return data.result;
    } catch (error) {
      console.log("调用模型失败" + error);
    }
  }
}
// 导出函数
module.exports = Conversation;